const App = getApp();

Page({
  data: {
    currentIndex: 0,
    category1: [],
    category2: [],
    category3: [],
    category4: [],
    // 分类列表
    categoryList: [],
    // 文章列表
    postList: [],
  },

  //swiper切换时会调用
  pagechange: function (e) {
    if ("touch" === e.detail.source) {
      console.log(this.data.currentIndex);
      let currentPageIndex = this.data.currentIndex
      currentPageIndex = (currentPageIndex + 1) % 3
      this.setData({
        currentIndex: currentPageIndex
      })
    }
  },
  //用户点击tab时调用
  titleClick: function (e) {
    let currentPageIndex =
      this.setData({
        //拿到当前索引并动态改变
        currentIndex: e.currentTarget.dataset.idx
      })
  },

  onLoad() {
    let _this = this;
    // 获取分类列表
    _this.getCategoryList();
  },

  onShow() {

  },


  /**
   * 获取分类主页数据
   * 1.所有分类列表
   * 2.所有商品列表
   */
  getCategoryList() {
    let _this = this;
    App._get('category_post/index', {}, result => {
      let data = result.data;
      _this.setData({
        categoryList: data['categoryList'],
        category1: data['newPostList']['10001'] || [],
        category2: data['newPostList']['10002'] || [],
        category3: data['newPostList']['10003'] || [],
        category4: data['newPostList']['10004'] || [],
      });
    });
  },


  /**
   * 跳转商品详情页
   */
  onTargetPost(e) {
    wx.navigateTo({
      url: '../postDetail/postDetail?post_id=' + e.currentTarget.dataset.id
    });
  },


  /**
   * 下拉到底加载数据
   */
  onDownLoad() {
    let _this = this;
    // 已经是最后一页
    if (_this.data.page >= _this.data.goodsList.last_page) {
      _this.setData({
        noMore: true
      });
      return false;
    }
    // 加载下一页列表
    _this.getGoodsList(true, ++_this.data.page);
  },


});
